/*
 * @Author: tkp
 * @Date: 2021-11-20 16:39:42
 */
// 导入axios实例
import request from './request'
import { Message } from 'element-ui';
import { code_status, setting } from './setting';
import router from '@/router'
import axios from 'axios'
import store from '@/store/index'
import loadEvents from './loading'
let loads = new loadEvents();
const http = {
    /**
    * methods: 请求方法
    * @param url 请求地址 
    * @param params 请求参数
    **/
    get(url, params) {
        const config = {
            method: 'get',
            url: url
        }
        if (params) config.params = params
        return new Promise(function (resolve, reject) {
            request(config).then(res => {
                if (res.code == code_status.SUCCESS) {
                    resolve(res)
                } else if (res.code == code_status.PASSTOKEN) {
                    window.localStorage.clear()
                    router.push('/login')
                    Message.error(res.msg)
                } else {
                    Message.error(res.msg)
                }
            }).catch(reson => {
                if (reson.request.status === 403) {
                    Message.error('暂无权限,请联系管理员')
                } else {
                    Message.error('服务器开小差了')
                }
            })
        })
    },
    post(url, params) {
        const config = {
            method: 'post',
            url: url,
            'Content-Type': 'application/json; charset=utf-8'
        }
        if (params) config.data = params
        return new Promise(function (resolve, reject) {
            request(config).then(res => {
                if (res.code == code_status.SUCCESS) {
                    resolve(res)
                } else if (res.code == code_status.PASSTOKEN) {
                    window.localStorage.clear()
                    router.push('/login')
                    Message.error(res.msg)
                } else {
                    Message.error(res.msg)
                }
            }).catch(reson => {
                if (reson.request.status === 403) {
                    Message.error('暂无权限,请联系管理员')
                } else {
                    Message.error('服务器开小差了')
                }
            })
        })
    },
    postBuffer(url, params) {
        const config = {
            method: 'post',
            url: url,
            'Content-Type': 'application/json; charset=utf-8',
        }
        if (params) config.data = params
        return new Promise((resolve, reject) => {
            loads.open()
            axios({
                method: 'post',
                url: setting() + url,
                data: params,
                responseType: 'blob',
                headers: {
                    'jwt-token': store.state.user.token,
                },
            }).then(res => {
                loads.close()
                if (res.status == '200') {
                    if (res.data.size) {
                        if (res.data.type == 'application/json') {
                            const xlsx = res.data
                            const blob = new Blob([xlsx])
                            let reader = new FileReader()
                            reader.readAsText(blob)
                            reader.onload = function (e) {
                                let data = JSON.parse(e.target.result)
                                Message.error(data.msg)
                            }
                        } else {
                            try {
                                var fileName = decodeURIComponent(res.headers["content-disposition"].split("=")[1])
                                const xlsx = res.data
                                const blob = new Blob([xlsx])
                                const url = window.URL.createObjectURL(blob)
                                const link = document.createElement('a')
                                link.href = url
                                link.setAttribute('download', fileName)
                                document.body.appendChild(link)
                                link.click()
                                document.body.removeChild(link)
                            } catch (error) {
                                Message.error('下载错误,请联系管理员')
                            }
                        }
                    } else {
                        Message.error(res.msg)
                    }
                }
            }).catch(err => {
                loads.close()
                if (err.request.status === 403) {
                    Message.error('暂无权限,请联系管理员')
                } else {
                    Message.error('服务器开小差了')
                }
                //reject(err)
            })
        })
    },
}
//导出
export default http